<script setup lang="ts">
import { onMounted, onBeforeUnmount } from "vue";
import useCatrgoryStore from "@/store/modules/category";
const categoryStore = useCatrgoryStore();
// 接收父组件传递过来的scene字段
defineProps(["scene"]);
// 一级分类的select框的选中的值发生改变时的回调
const handler1 = () => {
  // 清空二级分类id和三级分类数组以及id
  categoryStore.c2Id = "";
  categoryStore.c3Arr = [];
  categoryStore.c3Id = "";
  categoryStore.getC2();
};
// 二级分类的select框的选中的值发生改变时的回调
const handler2 = () => {
  // 清空三级分类id
  categoryStore.c3Id = "";
  categoryStore.getC3();
};
// onMounted钩子
onMounted(() => {
  categoryStore.getC1();
});
// onBeforeUnmount钩子
onBeforeUnmount(() => {
  categoryStore.$reset();
});
</script>

<template>
  <el-card>
    <el-form inline>
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene == 1 || scene == 2 ? true : false"
          v-model="categoryStore.c1Id"
          @change="handler1"
        >
          <el-option
            v-for="(c1, index1) in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 1 || scene == 2 ? true : false"
          v-model="categoryStore.c2Id"
          @change="handler2"
        >
          <el-option
            v-for="(c2, index2) in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 1 || scene == 2 ? true : false"
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="(c3, index3) in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped></style>
